package com.charmzenus.champion.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.charmzenus.champion.base.MyApplication;

public class UIUtils {
    /**
     * 获取到字符数组
     *
     * @param tabNames 字符数组的id
     */
    public static String[] getStringArray(int tabNames) {
        return getResource().getStringArray(tabNames);
    }

    public static Resources getResource() {
        return MyApplication.getApplication().getResources();
    }

    public static Context getContext() {
        return MyApplication.getApplication();
    }

    /**
     * 把Runnable 方法提交到主线程运行
     *
     * @param runnable
     */
    public static void runOnUiThread(Runnable runnable) {
        // 在主线程运行
        if (android.os.Process.myTid() == MyApplication.getMainTid()) {
            runnable.run();
        } else {
            //获取handler
            MyApplication.getHandler().post(runnable);
        }
    }

    public static View inflate(int id) {
        return View.inflate(getContext(), id, null);
    }

    public static Drawable getDrawalbe(int id) {
        return getResource().getDrawable(id);
    }

    public static int getDimens(int homePictureHeight) {
        return (int) getResource().getDimension(homePictureHeight);
    }

    /**
     * 延迟执行 任务
     *
     * @param run  任务
     * @param time 延迟的时间
     */
    public static void postDelayed(Runnable run, int time) {
        MyApplication.getHandler().postDelayed(run, time); // 调用Runable里面的run方法
    }

    /**
     * 取消任务
     *
     * @param auToRunTask
     */
    public static void cancel(Runnable auToRunTask) {
        MyApplication.getHandler().removeCallbacks(auToRunTask);
    }


    /**
     * 设置textview 确保不会因为空指针闪退
     * @param text
     * @param tv
     */
    public static void setTextView(String text, TextView tv)
    {
        if (TextUtils.isEmpty(text))
        {
            return;
        }
        if (tv == null)
        {
            return;
        }
        tv.setText(text);
    }

    /**
     * 设置button的文本信息
     * @param text
     * @param btn
     */
    public static void setButtonText(String text, Button btn)
    {
        if (TextUtils.isEmpty(text))
        {
            return;
        }
        if (btn == null)
        {
            return;
        }
        btn.setText(text);
    }

    /**
     * 跳转到下一个界面
     * @param currAct 当前界面
     * @param nextClazz 下一个界面的class文件
     * @param isFinish 是否关闭当前界面
     */
    public static <T> void skipToNextActivity(Activity currAct, Class<T> nextClazz, boolean isFinish)
    {
        Intent intent = new Intent(currAct, nextClazz);
        currAct.startActivity(intent);
        if (isFinish)
        {
            currAct.finish();
        }
    }

    /**
     * 跳转到下一个界面
     * @param currAct 当前界面
     * @param nextClazz 下一个界面的class文件
     * @param bundle 需要传递的bundle值
     * @param bundleKey bundle的key
     * @param isFinish 是否关闭当前界面
     */
    public static <T> void skipToNextActivity(Activity currAct, Class<T> nextClazz, Bundle bundle,
                                              String bundleKey, boolean isFinish)
    {
        Intent intent = new Intent(currAct, nextClazz);
        intent.putExtra(bundleKey, bundle);
        currAct.startActivity(intent);
        if (isFinish)
        {
            currAct.finish();
        }
    }


    public static void dissDialog(Dialog dialog)
    {
        if (dialog != null && dialog.isShowing())
        {
            dialog.dismiss();
        }
    }

}
